#include"head.h"
// userid 
bool check_userid(const char* user_id) {



    int len = strlen(user_id);
    int index = 0;
    int flag = 0;
    for (int index = 0; index < len; ++index) {
        if (!(user_id[index] >= '0' && user_id[index] <= '9') || user_id[index] == ' ') {
            printf("格式错误请输入数字\n");
            int c;
            while ((c = getchar()) != '\n' && c != EOF);
            return false;
        }
    }

    return true;

}
int update_user(client_status* user, c_sends* sends)
{
    strcpy(user->user_id, sends->user_id);
    strcpy(user->user_name, sends->user_name);
    strcpy(user->password, sends->password);
    user->islogin = 1;//登录了
    strcpy(user->path, "");

}
int update_sends(c_sends* sends, const char* user_id, const char* user_name, const char* password) {
    strcpy(sends->user_id, user_id);
    strcpy(sends->user_name, user_name);
    strcpy(sends->password, password);
    //sends->isstatus=0;
    return 0;
}
bool login(int *socket_fd, client_status* user, c_sends sends,char* token) {
    printf("isstatus:%d\n", sends.isstatus);
    recvMsg Msg;
    bzero(&Msg, sizeof(Msg));
    //   user=(client_status*)malloc(sizeof(client_status));
    if (sends.isstatus == 1) {
        printf("-------------登录--------------\n");
        fflush(stdout);
    }
    else if (sends.isstatus == 0) {
        printf("-------------注册--------------\n");
        fflush(stdout);

    }
    
        printf("请输入用户id(最多5位): ");
        fflush(stdout);
        char user_id[1024] = {0};
        char user_name[100] = {0};
        char password[100] = {0};

        int read_id = read(STDIN_FILENO, user_id, sizeof(user_id));

        if (read_id == -1) {
            perror("read_err");
        }
        user_id[read_id - 1] = '\0';
        printf("!!!!%s\n", user_id);
        //strlen是不包括\0 但是里面有\n 所以要-1 
        //将\n换成\0
        int len = strlen(user_id);
        user_id[5] = '\0';
        //

        if (!check_userid(user_id)) {
           // continue;
            return false;
        }

        printf("name_id:%s!\n", user_id);
        // printf("%c\n",name[0]);
        if(sends.isstatus==0){
            printf("请输入你的用户名: ");
            fflush(stdout);
            int read_name = read(STDIN_FILENO, user_name, sizeof(user_name));
            int len_name = strlen(user_name);
            user_name[len_name - 1] = '\0';
            len_name -= 1;
            printf("user_name %s\n", user_name);
        }

        printf("请输入您的密码:");
        fflush(stdout);
        int ret_read = read(STDIN_FILENO, password, sizeof(password));
        //printf("pswread : %d\n", ret_read);
        if (ret_read == -1) {

        }
        len = strlen(password);
        //len-1 的原因是  将最后赋值  （len个 是0-len-1)
        password[len - 1] = '\0';

        //
        update_sends(&sends, user_id, user_name, password);
        //拿到连接
        connect_client(socket_fd);

        //TODO  已经修改:登录注册给服务端发送字符0，方便服务端处理
        strcpy(sends.token,"");
        
        int user_send = send(*socket_fd, &sends, sizeof(sends), MSG_NOSIGNAL);
        printf("send: %d\n", user_send);
        if (user_send == -1) {
            // LOG(user,msg);
            perror("send_user_err");
            printf("send_user_err\n");
        }
        int user_recv = recv(*socket_fd, &Msg, sizeof(Msg),MSG_WAITALL);
        printf("Msg.token:%s\n",Msg.token);
        printf("--------------\n");
        printf("!!!statu: %s %d\n", Msg.statu,user_recv);
        if (user_recv == -1) {
            perror("recv_user_err");
            printf("recv_user_err");
        }
        else if (user_recv == 0) {
            // 连接被关闭
            printf("Connection closed by peer\n");
            // 处理连接关闭，如重试连接或清理资源等
        }
        //写死测试 
        // strcat(Msg.statu,"1");
        printf("statu: %s\n", Msg.statu);
        if ( strcmp(Msg.statu, "1") == 0) {
            if(sends.isstatus == 0){
                printf("%s注册成功\n", user->user_name);
                //成功再更新用户端的数据
                update_user(user, &sends);
                printf(" id: %sname :%spassword: %s\n", user->user_id, user->user_name, user->password);
                printf("请登录您的账户：\n"); 
                sends.isstatus=1;
                return true;
            }
            else if(sends.isstatus == 1){
                update_user(user, &sends);
                strcpy(user->user_name,Msg.msg);
                printf("%s %s登陆成功\n", user->user_id,user->user_name);
                


                //TODO +token--->send_mag中
                strcpy(token,Msg.token);
                

                printf(" id: %sname :%spassword: %s\n", user->user_id, user->user_name, user->password);
                return false;
            }
        }
        else {
            if (sends.isstatus == 0)
            {
                
                printf("注册失败，请重新输入\n");
                close(*socket_fd);
                return true;
            }
            else if (sends.isstatus == 1) {
                printf("登录失败，请重新输入\n");
                close(*socket_fd);
                return true;
            }
        }

    

    return false;
}



bool unlogin(sendMsg msg, client_status* user,int socket_fd) {
    recvMsg Msg;
    msg.mode = -1;
    int ret_send = send(socket_fd, &msg, sizeof(msg), MSG_NOSIGNAL);
    if(ret_send==-1){
        printf(" send_err\n");
        return 0;
    }
    int ret_recv = recv(socket_fd, &Msg, sizeof(Msg), MSG_WAITALL);
    if(ret_recv==-1){
        printf("send_err\n");
        return 0;
    }
    if (strcmp(Msg.statu, "1") == 0) {
        printf("信息:%s\n",msg.msg);
        user->islogin = 0;
        return 1;
    }
    return 0;
}
